package org.example.servise.impl;

import org.example.dao.UserDao;
import org.example.dao.impl.UserDaoimpl;
import org.example.pojo.User;
import org.example.servise.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;

@Service
public class UserServiceimpl implements UserService {
    //应用程序运行时，会自动的查询该类型的bean对象，并注入给当前成员变量
    @Autowired
    private UserDao userDao;

    public List<User> findAll() {
        //1.调用dao，获取数据
        List<String> lines = userDao.findAll();

        //2.解析用户信息，封装为用户对象 ->list
        List<User> usersList = lines.stream().map(line ->{
            String[] parts = line.split(",");
            Integer id = Integer.parseInt(parts[0]);
            String username = parts[1];
            String password = parts[2];
            String name = parts[3];
            Integer age = Integer.parseInt(parts[4]);
            LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new User(id,username,password,name,age,updateTime);
        }).toList();

        return usersList;
    }


}
